import axios from "axios";
import {ElMessage} from "element-plus";
import router from "~/router/index.js";



axios.interceptors.response.use(resp => {
        //成功执行的代码
        if (resp.data === "无权限访问！") {
            ElMessage.error("无权限访问!")
            router.push({path:"/login"})
            return;
            }
            return resp;
        }
)
export const doGet = (url, data, callback) => {
    axios({
        url: url,
        params: data, //这个参数在请求的地方栏后面
        method: "GET"
    }).then(resp => {
        if(typeof callback === 'function'){
            callback(resp.data)
        }
    }).catch(error => {

    })
}


export const doPost = (url, data, callback) => {
    axios({
        url: url,
        data: data, //这个参数在请求体中
        method: "POST"
    }).then(resp => {
        if(typeof callback === 'function'){
            callback(resp.data)
        }
    }).catch(error => {

    })
}

export const doLogin = (url, data, callback) => {
    axios({
        url: url,
        data: data, //这个参数在请求体中
        method: "POST",
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    }).then(resp => {
        if(typeof callback === 'function'){
            callback(resp.data)
        }
    }).catch(error => {

    })
}


export const doPut = (url, data, callback) => {
    axios({
        url: url,
        data: data,
        method: "PUT"
    }).then(resp => {
        if(typeof callback === 'function'){
            callback(resp.data)
        }
    }).catch(error => {

    })
}


export const doDelete = (url, data, callback) => {
    let paramInfo = "";
    if(data){
        paramInfo += "?"
        Object.keys(data).forEach(key => paramInfo += key + "=" + data[key] + "&")
    }
    axios({
        url: url + paramInfo,
        params: {},
        method: "DELETE"
    }).then(resp => {
        if(typeof callback === 'function'){
            callback(resp.data)
        }
    }).catch(error => {

    })
    }
